﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Measures;

namespace RaisingForce.Raise.Presentation.ViewModel.Specialized
{
    sealed public class SettingMeasures :
        ISettingMeasures
    {
        #region Constructors

        public SettingMeasures()
        { }

        public SettingMeasures(ISettingMeasures settingMeasures)
        {
            Contract.Requires(settingMeasures != null);
            this.distanceMeasure = settingMeasures.DistanceMeasure;
            this.angleMeasure = settingMeasures.AngleMeasure;
            this.timeMeasure = settingMeasures.TimeMeasure;
            this.angleTextFormat = settingMeasures.AngleTextFormat;
            this.angleMeasureFormatString = settingMeasures.AngleMeasureFormatString;
        }

        #endregion

        #region ISettingMeasures Members

        private IDistanceMeasure distanceMeasure =
            DistanceMeasures.Centimetre;

        public IDistanceMeasure DistanceMeasure
        {
            get { return distanceMeasure; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("DistanceMeasure");
                }
                Contract.EndContractBlock();
                this.distanceMeasure = value;
            }
        }

        private IAngleMeasure angleMeasure =
            AngleMeasures.Degree;

        public IAngleMeasure AngleMeasure
        {
            get { return angleMeasure; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("AngleMeasure");
                }
                Contract.EndContractBlock();
                this.angleMeasure = value;
            }
        }

        private ITimeMeasure timeMeasure =
            TimeMeasures.Millisecond;

        public ITimeMeasure TimeMeasure
        {
            get { return timeMeasure; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("TimeMeasure");
                }
                Contract.EndContractBlock();
                this.timeMeasure = value;
            }
        }

        public string DistanceMeasureText
        {
            get { return this.DistanceMeasure.ShortName; }
        }

        public string AngleMeasureText
        {
            get
            {
                return MessageHelper.TemplateIsValid(this.AngleMeasureFormatString, paramCount: 1) ?
                    string.Format(this.AngleMeasureFormatString, this.AngleMeasure.ShortName) :
                    this.AngleMeasure.ShortName;
            }
        }

        public string TimeMeasureText
        {
            get { return this.TimeMeasure.ShortName; }
        }

        private string angleTextFormat = string.Empty;

        public string AngleTextFormat
        {
            get { return this.angleTextFormat; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("AngleTextFormat");
                }
                Contract.EndContractBlock();
                this.angleTextFormat = value;
            }
        }

        private string angleMeasureFormatString = "{0}";

        public string AngleMeasureFormatString
        {
            get { return this.angleMeasureFormatString; }
            set
            {
                if (!MessageHelper.TemplateIsValid(value, paramCount: 1))
                {
                    throw new ArgumentException(
                        string.Format(
                            "AngleMeasureFormatString property \"{0}\" is not a valid format string.",
                            value));
                }
                Contract.EndContractBlock();
                this.angleMeasureFormatString = value;
            }
        }

        #endregion
    }
}
